package atmgui;

import atm.FileUtil;
import atm.User;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 取钱
 */

public class GetPanel extends JPanel implements ActionListener {
    JTextField usrTextField = new JFormattedTextField();

    private JButton offbtn = new JButton(new ImageIcon("src/images/buttonOff.jpg"));

    private ATM05 atm05;
    private Font font = new Font("宋体", Font.PLAIN, 25);
    private JButton surebtn = new JButton(new ImageIcon("src/images/buttonSure.jpg"));

    public GetPanel(ATM05 atm05) {


        JLabel label = new JLabel("请输入取款金额:");
        label.setBounds(100, 200, 300, 50);
        label.setFont(font);//字体大小
        label.setForeground(Color.BLUE);//字体颜色
        //文本框组件

        usrTextField.setBounds(300, 200, 300, 50);

        surebtn.setBounds(250, 300, 140, 50);
        surebtn.addActionListener(this);


        offbtn.setBounds(500, 300, 140, 50);
        offbtn.addActionListener(this);


        this.add(offbtn);
        this.add(surebtn);
        this.add(label);
        this.add(usrTextField);


        this.atm05 = atm05;
        this.setLayout(null);
        this.setOpaque(false);


    }


    @Override
    public void actionPerformed(ActionEvent e) {
        if (surebtn == e.getSource()) {
            User user = atm05.getLoginUser();
            if (user.getBlance()>Double.parseDouble(usrTextField.getText())) {

                user.setBlance(user.getBlance() - Double.parseDouble(usrTextField.getText()));
                JOptionPane.showMessageDialog(this, "取款成功");
                usrTextField.setText("");
                FileUtil.writeObject(atm05.getUsers(),ATM05.FILE_PATH);
            }else if (user.getBlance()<Double.parseDouble(usrTextField.getText())){
                JOptionPane.showMessageDialog(this,"余额不足，取款失败");
            }


            atm05.showPanel("menu");

        }
        else if (e.getSource()==offbtn){
            atm05.showPanel("menu");
        }
    }
}
